//
//  YSCardGridItemFrame.m
//  Yueshi
//
//  Created by Northwan on 15/7/20.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSCardGridItemFrame.h"

#define ImageInset 5

@implementation YSCardGridItemFrame

/**
 *  思路：同时计算两个商品的Frame，传入两个model,这个思路不好，我打算有时间了把这个代码重构，重构的思路是使用一个collectionView来实现，如果这样实现的话应该会优化一些
 *
 *  @param advert
 */
-(void)setAdverts:(NSArray *)adverts{
    _adverts = adverts;
    YSAdvert *advert = adverts[0];
    //商品图片1
    self.imageFrame1 = CGRectMake(YSPublicXInset, 0, (SCREENW/2)-ImageInset*2, 140);
    self.cellHeight = CGRectGetMaxY(self.imageFrame1);
    
    if(!ISBLANK(advert.advertText)){
        //商品描述1
        CGSize descSize = [advert.advertText sizeWithFont:SYS_FONT(DescFont) constrainedToSize:CGSizeMake(self.imageFrame1.size.width,MAXFLOAT)];
        CGFloat descX = CGRectGetMinX(self.imageFrame1);
        CGFloat descY = CGRectGetMaxY(self.imageFrame1)+7;
        self.descFrame1 = (CGRect){{descX, descY}, descSize};
        self.cellHeight = CGRectGetMaxY(self.descFrame1)+YSPublicXInset;
    }
    
    if(adverts.count == 2){
        YSAdvert *advert = adverts[1];
        //商品图片2
        self.imageFrame2 = CGRectMake(CGRectGetMaxX(self.imageFrame1)+ImageInset, CGRectGetMinY(self.imageFrame1), (SCREENW/2)-ImageInset-10, 140);
        
        if(!ISBLANK(advert.advertText)){
            //商品描述2
            CGSize descSize2 = [advert.advertText sizeWithFont:SYS_FONT(DescFont) constrainedToSize:CGSizeMake(self.imageFrame1.size.width,MAXFLOAT)];
            CGFloat descX2 = CGRectGetMinX(self.imageFrame2);
            CGFloat descY2 = CGRectGetMaxY(self.imageFrame2)+7;
            self.descFrame2 = (CGRect){{descX2, descY2}, descSize2};
        }
    }
    
    //自己的frame
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = SCREENW;
    CGFloat h = self.cellHeight + YSPublicXInset*2;
    self.frame = CGRectMake(x, y, w, h);
}

@end
